package com.java2007.zhangyu.restaurant.controller.backend;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.pojo.Food;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodService;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.impl.FoodServiceImpl;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;
import com.java2007.zhangyu.restaurant.utils.FileUtil;
import com.java2007.zhangyu.restaurant.utils.WebUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.util.List;
import java.util.Map;

/**
 * 菜品控制器
 * @author zy
 * @Version
 * @date 2021/01/26
 */
@WebServlet("/food")
@MultipartConfig
public class FoodController extends BaseServlet {

    private FoodService foodService = new FoodServiceImpl();
    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    /**
     * 根据菜品 fId 删除菜品信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String del(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String fIdStr = request.getParameter("fId");
        //将字符串类型转换为整型
        int fId = Integer.parseInt(fIdStr);
        //根据菜类别名模糊查询菜类别的信息
        foodService.deleteByFoodId(fId);
        //重定向到查询接口
        return ServletConstant.PRE_REDIRECT + "/food?method=search";
    }

    /**
     * 根据菜品 fId 查询
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryByFId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //查询菜类别
        List<FoodType> types = foodTypeService.queryAll();
        request.setAttribute("types",types);

        //获取请求参数
        String fIdStr = request.getParameter("fId");
        //将字符串类型转换为整型
        int fId = Integer.parseInt(fIdStr);
        //根据菜品名id查询菜类别的信息
        Food food = foodService.queryByFoodId(fId);
        //将查询到的信息放到 request域中
        request.setAttribute("food", food);
        //转发
        return ServletConstant.PRE_FORWARD + "/backend/detail/food/food-update.jsp";
    }

    /**
     * 查询菜类别到保存页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryTypeAll(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<FoodType> foodTypes = foodTypeService.queryAll();
        request.setAttribute("foodTypes",foodTypes);

        return ServletConstant.PRE_FORWARD + "/backend/detail/food/food-save.jsp";
    }

    /**
     * 检验添加的菜品是否存在的方法
     * @param request
     * @param response
     */
    public String checkFoodName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数菜品名
        String fName = request.getParameter("fName");
        //根据参数查找该菜品是否存在
        int result = foodService.checkFoodName(fName);
        return String.valueOf(result);
    }

    /**
     * 添加菜品信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //调用文件上传工具类
        String fileName = FileUtil.upload(request);

        if(StringUtils.isEmpty(fileName)) {
            //文件上传错误
            return "文件上传错误";
        }
        //获取请求多个参数，调用工具类 WebUtil
        Food food = WebUtil.copyParamToBean(request.getParameterMap(), new Food());

        List<Food> foods = foodService.queryAll();
        for (Food f:foods) {
            if (f.getfName().equals(food.getfName())){
                //重定向
                return "redirect:/food?method=search";
            }
        }
        food.setfImage(fileName);
        foodService.save(food);
        //重定向
        return "redirect:/food?method=search";
    }

    /**
     * 搜索
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<FoodType> foodTypes = foodTypeService.queryAll();
        request.setAttribute("foodTypes",foodTypes);

        //获取请求参数
        String fName = request.getParameter("fName");
        //根据菜类别名模糊查询菜类别的信息
        List<Food> foods = foodService.queryByFoodName(fName);
        //将查询到的信息放入request域中
        request.setAttribute("foods", foods);
        request.setAttribute("fName", fName);
        //转发
        return ServletConstant.PRE_FORWARD +"/backend/detail/food/food-list.jsp";
    }

    /**
     * 更新菜品图片
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String fileName = FileUtil.upload(request);

        if(StringUtils.isEmpty(fileName)) {
            //文件上传错误
            return "文件上传错误";
        }

        //获取请求多个参数，调用工具类 WebUtil
        Food food = WebUtil.copyParamToBean(request.getParameterMap(), new Food());

        //把图片存为一个服务上的映射资源的路径
        food.setfImage(fileName);

        foodService.update(food);
        //重定向到查询接口
        return ServletConstant.PRE_REDIRECT + "/food?method=search";

    }
}
